#include "buttons.h"

u8
buttons_get(void)
{
        u8 xy = 0x0, flag = 0;

redetect_matrix:
        /*矩阵按键*/
        P1 = 0xf0;
        if (P1 == 0xf0)
                goto leave_detect_matrix;
        if (flag)
                goto redetect_matrix;
        if (P1_4 == 0)
                xy |= 0x1;
        else if (P1_5 == 0)
                xy |= 0x2;
        else if (P1_6 == 0)
                xy |= 0x4;
        else if (P1_7 == 0)
                xy |= 0x8;
        P1 = 0x0f;
        if (P1_0 == 0)
                xy |= 0x80;
        else if (P1_1 == 0)
                xy |= 0x40;
        else if (P1_2 == 0)
                xy |= 0x20;
        else
                xy |= 0x10;
        flag = 1;
        goto redetect_matrix;

leave_detect_matrix:
        if (!flag)
                return BUTTON_TYPE_NONE;
        switch (xy) {
        case 0x18:
                return 7;
        case 0x28:
                return 8;
        case 0x48:
                return 9;
        case 0x88:
                return BUTTON_TYPE_SETTINGS;

        case 0x14:
                return 4;
        case 0x24:
                return 5;
        case 0x44:
                return 6;
        case 0x84:
                return BUTTON_TYPE_PAGE_UP;

        case 0x12:
                return 1;
        case 0x22:
                return 2;
        case 0x42:
                return 3;
        case 0x82:
                return BUTTON_TYPE_PAGE_NEXT;
        
        case 0x11:
                return BUTTON_TYPE_NONE;
        case 0x21:
                return 0;
        case 0x41:
                return BUTTON_TYPE_DELETE;
        case 0x81:
                return BUTTON_TYPE_RETURN;

        default:
                return BUTTON_TYPE_NONE;
        }
}
